package com.reactsdk.sid.button

import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.widget.FrameLayout
import androidx.annotation.ColorInt
import com.facebook.react.bridge.Callback
import com.facebook.react.bridge.ReadableMap
import com.reactsdk.sid.SIDUtils.getParamOrException
import com.reactsdk.sid.SIDUtils.getParamOrNull
import sberid.sdk.auth.model.ButtonText
import sberid.sdk.auth.model.ButtonType
import sberid.sdk.auth.model.SberIDButtonModel
import sberid.sdk.auth.view.SberIDButton

class SIDButtonView(context: Context) : FrameLayout(context) {

	init {
		addView(
			SberIDButton(context, sberIDButtonModel = null).apply {
				layoutParams = LayoutParams(
					android.view.ViewGroup.LayoutParams.MATCH_PARENT,
					android.view.ViewGroup.LayoutParams.MATCH_PARENT,
				)
				foregroundGravity = Gravity.CENTER
			}
		)

	}

	private var sberIDButtonModel: SberIDButtonModel? = null
	val sidButonView: SberIDButton by lazy {
		SberIDButton(context, sberIDButtonModel = sberIDButtonModel)
	}

	fun setButtonParams(options: ReadableMap, promise: Callback){
		val clientId: String? = options.getParamOrException("client_id", promise)
		val width: Int? = options.getParamOrNull("width", promise)
		val height: Int? = options.getParamOrNull("height", promise)
		val type: ButtonType = when(options.getParamOrNull<String>("type", promise)){
			"white_type" -> ButtonType.WHITE_TYPE
			else -> ButtonType.DEFAULT_TYPE
		}
		val text: ButtonText = when(options.getParamOrNull<String>("button_text", promise)){
			"login_short" -> ButtonText.LOGIN_SHORT
			"continue" -> ButtonText.CONTINUE
			"fill" -> ButtonText.FILL
			else -> ButtonText.LOGIN
		}
		val cornerRadius: Int = options.getParamOrNull("corner_radius", promise) ?: 0
		@ColorInt val strokeColor: Int = Color.rgb(76, 76, 76)
		val buttonLoader: Boolean = false
		val sidButtonParams = SberIDButtonModel(
			clientId = clientId,
			width = width,
			height = height,
			type = type,
			text = text,
			cornerRadius = cornerRadius,
			strokeColor = strokeColor,
			buttonLoader = buttonLoader
		)

		sberIDButtonModel = sidButtonParams
	}
}